/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Action;

import Model.User;
import exception.DmsException;

/**
 *
 * @author griever
 */
public abstract class BaseAjax extends Fly.Web.Action.Handler {
    public String json;
    public Integer userId;
    private User user;
    
    @Override
    public void Handle() {
        userId = null;
        try {
            userId = Integer.parseInt(this.getSession("loginId").toString());
        } catch (NumberFormatException ex)  {
            
        }
        
        setTemplate("/html/base.html");
        try {
            Object result = this.doHandle();
            json = Fly.Util.Serializer.Json.Encode(new AjaxResult(result));
        } catch (Exception ex)  {
            json = Fly.Util.Serializer.Json.Encode(new DmsException("Server Error: "+ex.getMessage()));
        }
    }
    
    public User getUser()   {
        if (this.user == null)  {
            this.user = new User();
            this.user.id = this.userId;
            if (!this.user.Instance().Status)   {
                this.user = null;
            }
        }
        return this.user;
    }
    
    public Integer getUserId()  {
        return this.userId;
    }
    
    public abstract Object doHandle();
}

class AjaxResult {
    public Object result;
    
    public AjaxResult(Object result)    {
        this.result = result;
    }
}